<?php
include_once '../classes/design.php';
include_once '../classes/clienteServicos.php';
include_once '../../classes/erro.php';
include_once '../../classes/marca.php';
include_once '../../classes/manipuladorXML.php';

// Salvar, se tiver pedido pra salvar
if (isset($_REQUEST["salvar"])) {
	$params["id"] = $_REQUEST["id"];
	$params["nome"] = $_REQUEST["nome"];
	$xml = ClienteServicos::requisitar("marcas/salvar.php", $params);
	$marca = ManipuladorXML::desserializar($xml);
	if (gettype($marca) == "object" && get_class($marca) == "Erro") {
		header("Location: index.php?erro=" . $marca->get_msg());
		exit;
	}
	else {
		header("Location: index.php?sucesso=Marca " . $marca->get_nome() . " salva!");
		exit;
	}
}

// Recuperar o objeto
if (isset($_REQUEST["id"])) {
	$xml = ClienteServicos::requisitar("marcas/get.php", array("id" => $_REQUEST["id"]));
	$marca = ManipuladorXML::desserializar($xml);
	if (gettype($marca) == "object" && get_class($marca) == "Erro") {
		header("Location: index.php?erro=" . $marca->get_msg());
		exit;
	}
}

// Criar novo objeto, caso não tenha sido instanciado
if (!isset($marca)) {
	$marca = new Marca();
}

$design = new Design("..");
$design->imprimir_topo();

echo "
	<h1>Marcas</h1>
";

if ($marca->get_id() == -1) {
	echo "<h2>Nova marca</h2>";
}
else {
	echo "<h2>Alterar marca</h2>";
}

echo "
	<form method=\"post\" action=\"" . $_SERVER["PHP_SELF"] . "\">
		<input type=\"hidden\" name=\"id\" value=\"" . $marca->get_id() . "\" />
		<input type=\"hidden\" name=\"salvar\" value=\"1\" />
		<table border=\"0\" align=\"center\">
			<tr>
				<td align=\"right\">Nome</td>
				<td>
					<input type=\"text\" name=\"nome\" value=\"" . $marca->get_nome() . "\"/>
				</td>
			</tr>
			<tr>
				<td colspan=\"2\" align=\"center\">
					<button type=\"submit\">Salvar</button>
					<button type=\"button\" onclick=\"javascript: location.href='index.php';\">Cancelar</button>
				</td>
			</tr>
		</table>
	</form>
";

$design->imprimir_rodape();

?>